package org.hospital.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.hospital.entity.BillProject;
import org.hospital.entity.BillProjectDAO;
import org.hospital.entity.BillProjectId;
import org.hospital.entity.MedicalCard;
import org.hospital.entity.Project;
import org.hospital.entity.ProjectDAO;
import org.hospital.service.BillProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@Service
public class BillProjectServiceImpl implements BillProjectService {

	@Autowired
	private BillProjectDAO bpDao;
	@Autowired
	private ProjectDAO pDao;

	@Override
	public BillProject getById(BillProjectId billProjectId) {
		return bpDao.findById(billProjectId);
	}

	@Override
	public BillProject addEntity(BillProject entity) {
		return bpDao.merge(entity);
	}

	@Override
	public BillProject saveEntity(BillProject entity) {
		return bpDao.merge(entity);
	}

	@Override
	public List<BillProject> addEntityList(List<BillProject> bpList) {
		List<BillProject> result = new ArrayList<BillProject>();
		
		for (int i = 0; i < bpList.size(); i++) {
			BillProject bp = bpList.get(i);
			result.add(bpDao.merge(bp));
		}

		return result;
	}

	@Override
	public JSONArray getBillDetailTable(int billId) {
		// TODO 单据表详情
		JSONArray resultJA = new JSONArray();
		List<BillProject> bpList = bpDao.findByIdList(billId);
		for(int i=0;i<bpList.size();i++){
			JSONObject resultOJ = new JSONObject();
			Project project = pDao.findById(bpList.get(i).getId().getProjectId());
			
			resultOJ.put("projectName", project.getName());
			resultOJ.put("number", bpList.get(i).getNumber());
			resultOJ.put("unitPrice", project.getUnitPrice());
			resultOJ.put("price", bpList.get(i).getPrice());
			resultOJ.put("doctorCost", bpList.get(i).getDoctorCost());
			resultOJ.put("clinicCost", project.getClinicCost());
			resultOJ.put("excutorCost", project.getExcutorCost());
			resultOJ.put("extraInfo", bpList.get(i).getExtraInfo());
			
			resultJA.add(resultOJ);
		}
		return resultJA;
	}

}
